<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>账户设置</el-breadcrumb-item>
    </el-breadcrumb>
    <div style="width: 400px">

      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-row class="demo-avatar demo-basic">
          <el-col :span="12">
            <div class="demo-basic--circle" align="center" style="width: 499px">
              <div class="block">
              </div>
              <div>
                <el-upload class="avatar-uploader" action="http://localhost:88/api/system/upload/oss?folder=myimages"
                  :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                  <img v-if="imageUrl" :src="imageUrl" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                <el-button type="text" @click="uploadimg()">确认上传</el-button>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-form-item label="用户名" prop="admin">
          <el-input type="input" v-model="list.loginName" autocomplete="off" :disabled="true"></el-input>
          <input type="hidden" v-model="list.parentId" prop="parentId" />
        </el-form-item>
        <el-form-item label="Email" prop="email">
          <el-input type="input" v-model="list.email" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="旧密码" prop="oldpass" :required="true">
          <el-input type="password" v-model="ruleForm.oldpass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass" :required="true">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass" :required="true">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    var validateOldPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入旧密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'));
      }
      // if (value.lenth() > 20) {
      //   callback(new Error('密码不能太长'))
      // }
      else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      list: {},
      parentId: 1,
      // 映射上传文件对应的文件路径
      imageUrl: '',
      ruleForm: {
        oldpass: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        oldpass: [
          { validator: validateOldPass, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      circleUrl: "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
    };
  },
  methods: {
    submitForm (ruleForm) {
      console.log(this.ruleForm)
      this.$refs[ruleForm].validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post(`/system/admin/updatepwd?parentId=${this.parentId}`, this.ruleForm)
          // console.log(res)
          alert(res.message);
          this.getAdminInfo()
        } else {
          this.$message.error('请将信息填写完整')
          return false;
        }
      });
    },
    async uploadimg () {
      // console.log(this.ruleForm)
      if (this.imageUrl != null) {
        this.ruleForm.image = this.imageUrl
        const { data: res } = await this.$http.post(`/system/admin/updatepwd?parentId=${this.parentId}`, this.ruleForm)
        alert(res.message);
        this.getAdminInfo();
      } else {
        this.$message.error('请先选中要上传的图片')
      }
    },
    // 获取用户信息
    async getAdminInfo () {
      // 发送请求查询数据并返回
      const parentId = this.parentId
      const { data: res } = await this.$http.get(`/system/admininfo/findByParentId?parentId=${parentId}`)
      this.list = res.data
      this.imageUrl = this.list.image
    },
    // 文件上传相关方法
    handleAvatarSuccess (res, file) {
      // 将后台写回的图片路径赋值给imageUrl
      this.imageUrl = file.response
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      // 图片大小不能大于2M
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  },
  // 钩子函数
  async created () {
    this.getAdminInfo()
  }
}
</script>

<style>
body {
  text-align: center;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>